/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-02-08     Zhangyihong  the first version
 * 2018-04-03     XY           gt9xx for 1024 * 600
 * 2018-04-14     liu2guang    optimize int and rst to pin framework
 * 2017-08-08     XY           imxrt1052
 * 2018-10-29     XY
 */

#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"
#include "drv_i2c.h"

#ifdef RT_USING_TOUCH

#define TP_INT_PIN GPIO_PORT_E,GPIO_PIN_3
#define TP_RST_PIN GPIO_PORT_E,GPIO_PIN_4

#ifndef TP_INT_PIN
#error "Please config touch panel INT pin."
#endif
#ifndef TP_RST_PIN
#error "Please config touch panel RST pin."
#endif

#ifndef IIC_RETRY_NUM
#define IIC_RETRY_NUM 2
#endif

#define TOUCH_I2C_NAME  "i2c0"

#if 1
#define TPDEBUG     rt_kprintf
#define LOG         rt_kprintf
#else
#define TPDEBUG(...)
#define LOG(...)
#endif

#include "drv_touch.h"
#include "drv_touch_i2c_port.h"

struct rt_i2c_bus_device *i2c_bus;

int touch_i2c_wr_bytes(uint8_t dev_addr, uint8_t * wbuf, int wlen, uint8_t * rbuf, int rlen)
{
    struct rt_i2c_msg msgs[2];
    int ret;

    msgs[0].addr = dev_addr;
    msgs[0].flags = RT_I2C_WR;
    msgs[0].len = wlen;
    msgs[0].buf = wbuf;

    msgs[1].addr = dev_addr;
    msgs[1].flags = RT_I2C_RD;
    msgs[1].len = rlen;
    msgs[1].buf = rbuf;
    //此处直接调用drv_i2c中的方法，请保证没有线程冲突
    //原始i2c_transfer(dev->i2c, msgs, 2) 如果在中断中调用，会因为锁问题停止
    ret = ingenic_i2c_xfer(i2c_bus, msgs, 2);//this must 2

    //return ret;// < 0 ? ret : (ret != 2 ? -EIO : 0);
    return ret;
}

int touch_i2c_write_bytes(uint8_t dev_addr, uint8_t * buf, int len)
{
    struct rt_i2c_msg msgs[2];
    int ret;

    msgs[0].addr = dev_addr;
    msgs[0].flags = RT_I2C_WR;
    msgs[0].len = len;
    msgs[0].buf = buf;

    msgs[1].addr = dev_addr;
    msgs[1].flags = RT_I2C_RD;
    msgs[1].len = len;
    msgs[1].buf = buf;
    //此处直接调用drv_i2c中的方法，请保证没有线程冲突
    //原始i2c_transfer(dev->i2c, msgs, 2) 如果在中断中调用，会因为锁问题停止
    ret = ingenic_i2c_xfer(i2c_bus, msgs, 2);//this must 2

    //return ret;// < 0 ? ret : (ret != 2 ? -EIO : 0);
    return ret;
}

int touch_i2c_read_bytes(uint8_t dev_addr, uint8_t * buf, int len)
{
    struct rt_i2c_msg msgs[2];
    int ret;

    msgs[0].addr = dev_addr;
    msgs[0].flags = RT_I2C_WR;
    msgs[0].len = len;
    msgs[0].buf = buf;

    msgs[1].addr = dev_addr;
    msgs[1].flags = RT_I2C_RD;
    msgs[1].len = len;
    msgs[1].buf = buf;
    //此处直接调用drv_i2c中的方法，请保证没有线程冲突
    //原始i2c_transfer(dev->i2c, msgs, 2) 如果在中断中调用，会因为锁问题停止
    ret = ingenic_i2c_xfer(i2c_bus, msgs, 2);//this must 2

    //return ret;// < 0 ? ret : (ret != 2 ? -EIO : 0);
    return ret;
}

int touch_i2c_init(void)
{
	rt_kprintf("%s\n",__FUNCTION__);
    i2c_bus = rt_i2c_bus_device_find(TOUCH_I2C_NAME);
    RT_ASSERT(i2c_bus);

    if(rt_device_open(&i2c_bus->parent, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
    {
        TPDEBUG("[TP] %s I2C not open error!\n", TOUCH_I2C_NAME);
    }
    return 0;
}

#endif
